package cn.com.cetccst.datacenter.message.entity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverter;
import androidx.room.TypeConverters;

import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import cn.com.cetccst.datacenter.DataUtils;

//我的-消息通知消息实体
@Entity(tableName = InboxMessageEntity.TABLE_NAME)
@TypeConverters({InboxMessageEntity.ActionListConverter.class, InboxMessageEntity.BitmapConverter.class})
public class InboxMessageEntity {
    public static final String TABLE_NAME = "inbox_message_entity";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private long id;
    @ColumnInfo(name = "title")
    private String title;
    @ColumnInfo(name = "app_id")
    private String appId;
    @ColumnInfo(name = "icon")
    private Bitmap icon;
    @ColumnInfo(name = "read")
    private int read;
    @ColumnInfo(name = "received_date")
    private long receivedDate;
    @ColumnInfo(name = "subject")
    private String subject;
    @ColumnInfo(name = "content")
    private String content;
    @ColumnInfo(name = "parameter")
    private String parameter;
    @ColumnInfo(name = "actions")
    private List<Action> actions;

//    public InboxMessageEntity() {
//    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public int getRead() {
        return read;
    }

    public void setRead(int read) {
        this.read = read;
    }

    public long getReceivedDate() {
        return receivedDate;
    }

    public void setReceivedDate(long receivedDate) {
        this.receivedDate = receivedDate;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getParameter() {
        return parameter;
    }

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public List<Action> getActions() {
        return actions;
    }

    public void setActions(List<Action> actions) {
        this.actions = actions;
    }

    public class Action {
        private String name;
        private String param;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }
    }

    public static class ActionListConverter {
        @TypeConverter
        public List<Action> stringToObject(String value) {
            Type typeToken = new TypeToken<List<String>>() {
            }.getType();
            return DataUtils.getGson().fromJson(value, typeToken);
        }

        @TypeConverter
        public String objectToString(List<Action> value) {
            return DataUtils.getGson().toJson(value);
        }
    }

    public static class BitmapConverter {
        @TypeConverter
        public byte[] bitmap2Bytes(Bitmap bitmap) {
            if (bitmap == null) {
                return new byte[0];
            }
            int size = bitmap.getWidth() * bitmap.getHeight() * 4;
            ByteArrayOutputStream out = new ByteArrayOutputStream(size);
            try {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
                return out.toByteArray();
            } catch (IOException e) {
                Log.w("BitmapConverter", "Could not write bitmap");
                return new byte[0];
            }
        }

        @TypeConverter
        public Bitmap bytes2Bitmap(byte[] bytes) {
            if (bytes == null || bytes.length == 0) {
                return null;
            }
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
        }
    }
}
